package com.jetinno.simple;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jetinno.utils.InputMethodUtil;
import com.jetinno.utils.LogUtils;
import com.jetinno.utils.ScreenUtil;
import com.jetinno.utils.SimpleCallback;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by zhangyuncai on 2021/7/29.
 */
public abstract class SimpleDialog extends Dialog {
    protected String TAG = this.getClass().getSimpleName();
    protected boolean hiddenSoft = false;//dialog显示后，是否隐藏键盘
    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    protected SimpleCallback sureCallback = null;
    protected SimpleCallback cancelCallback = null;

    public SimpleDialog(@NonNull Context context) {
        super(context);
    }

    public SimpleDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setWindowsStyle();
        setContentView(getLayoutId());
        findViewByIds();
        initView();
        initEvent();
        setCanceledOnTouchOutside(false);
    }

    protected void findViewByIds() {
    }

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(false);
    }

    protected void setWindowsStyle() {
        //去掉dialog的标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        /* 设置与屏幕等宽 */
        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        if (hiddenSoft) {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        }
        lp.width = getWidthStyle();
        lp.height = getHeightStyle();
        lp.gravity = getGravity();
        lp.dimAmount = 0.8f;//透明底
        window.setAttributes(lp);
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initEvent();

    public void onClick(View view) {

    }

    protected void addOnClick(int... ids) {
        for (int id : ids) {
            findViewById(id).setOnClickListener(v -> {
                onClick(v);
            });
        }
    }

    public <T> SimpleDialog setSureCallback(SimpleCallback<T> sureCallback) {
        this.sureCallback = sureCallback;
        return this;
    }

    public <T> SimpleDialog setCancelCallback(SimpleCallback<T> cancelCallback) {
        this.cancelCallback = cancelCallback;
        return this;
    }

    protected String getString(@StringRes int res) {
        return getContext().getResources().getString(res);
    }

    protected int getColor(@ColorRes int res) {
        return ContextCompat.getColor(getContext(), res);
    }

    protected void closeKeyBoard() {
        InputMethodUtil.closeKeyBoard(getContext(), getWindow());
    }

    protected void delay(long delay, SimpleCallback<Object> callback) {
        Disposable subscribe = Observable.timer(delay, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    callback.callback(true);
                }, throwable -> {
                    LogUtils.logException(TAG, throwable);
                });
        mCompositeDisposable.add(subscribe);
    }

    @Override
    public void show() {
        super.show();
        logLify("show");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        logLify("dismiss");
        mCompositeDisposable.dispose();
    }

    @FloatRange(from = 0.0, to = 1.0)
    protected double getWidthPercent() {
        return DialogPx.getWidthPercent();
    }

    protected int getWidthStyle() {
        double widthPercent = getWidthPercent();
        if (widthPercent > 0 && widthPercent < 1) {
            return (int) (ScreenUtil.getScreenWidth(getContext()) * widthPercent);
        }
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    @FloatRange(from = 0.0, to = 1.0)
    protected double getHeightPercent() {
        return 0;
    }

    protected int getHeightStyle() {
        double heightPercent = getHeightPercent();
        if (heightPercent > 0 && heightPercent < 1) {
            return (int) (ScreenUtil.getScreenHeigth(getContext()) * heightPercent);
        }
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    protected int getGravity() {
        return Gravity.CENTER;
    }

    /**
     * 打印生命周期
     *
     * @param state
     */
    private void logLify(String state) {
        LogUtils.logOperation(TAG, state);
    }
}
